﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ModemGSM;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmDial : FrmDefault
    {
        private TimeSpan _TmCall;
        
        private enum StatusPhoneEnum
        {
            Ready = 0,
            Ringing = 1,
            call = 2,
            Talk = 3
        }
        private StatusPhoneEnum _StatusPhone = StatusPhoneEnum.Ready;
        public FrmDial()
        {
            InitializeComponent();
            PnlSetting.Visible = false;
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BntSettings_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                PnlSetting.BringToFront();
                PnlSetting.Visible = true;
                BntSettings.Visible = false;
            }
        }

        private void FrmDial_Load(object sender, EventArgs e)
        {
            LstAgenda.Items.Clear();
            foreach (PhoneBook item in SharedLib.PhonesBook)
            {
                LstAgenda.Items.Add(item);
            }
            LstAgenda.SelectedIndex = -1;
            TxtNumber1.Text = Properties.Settings.Default.DefaultPhoneM1Number;
            TxtNumber2.Text = Properties.Settings.Default.DefaultPhoneM2Number;
            TxtNumber3.Text= Properties.Settings.Default.DefaultPhoneM3Number;
            TxtNome1.Text = Properties.Settings.Default.DefaultPhoneM1Name;
            TxtNome2.Text = Properties.Settings.Default.DefaultPhoneM2Name;
            TxtNome3.Text = Properties.Settings.Default.DefaultPhoneM3Name;
            TxtOPE.Text = Properties.Settings.Default.DefaultPhoneMDDD;
            toolTip.SetToolTip(BntM1, TxtNome1.Text != "" ? TxtNome1.Text: ResourcesText.WordMemory + " 1");
            toolTip.SetToolTip(BntM2, TxtNome2.Text != "" ? TxtNome2.Text : ResourcesText.WordMemory + " 2");
            toolTip.SetToolTip(BntM3, TxtNome3.Text != "" ? TxtNome3.Text : ResourcesText.WordMemory + " 3");
            toolTip.SetToolTip(BntOPE, TxtOPE.Text != "" ? ResourcesText.AddCode + " " + TxtOPE.Text : " " +  ResourcesText.HostOperator);
            SharedLib.ModemGSM.NoCallDetected  += new Modem.NoCallEventHandler(ModemGSM_NoCallDetected);
            LblMsg.Visible = false;
        }

        private void ModemGSM_NoCallDetected(object sender, NoCallEventArgs e)
        {
            SharedLib.IsNoCallDetected = e.status;
        }

        private void BntNumbers_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                LblNumDial.Text += ((Button)sender).Text;
                this.LstAgenda.Focus();
            }
        }

        private void BntClear_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                LblNumDial.Text = "";
                this.LstAgenda.Focus();
            }
        }

        private void BntCE_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                if (LblNumDial.Text == "") return;
                LblNumDial.Text = LblNumDial.Text.Substring(0, LblNumDial.Text.Length - 1);
            }
        }

        private void BntCall_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                if (LblNumDial.Text == "")
                {
                    LblMsg.Visible = false;
                    return;
                }
                _TmCall = new TimeSpan(0, 0, 0);
                LblMsg.Text = "";
                LblMsg.Visible = true;
                BntCall.Text = ResourcesText.WordCall;
                SharedLib.IsNoCallDetected = NoCallEventEnum.None;
                timerCall.Start();
                Application.DoEvents();
                SharedLib.ModemGSM.Call(LblNumDial.Text);
                _StatusPhone = StatusPhoneEnum.call;
            }
            else if (_StatusPhone == StatusPhoneEnum.call)
            {
                timerCall.Stop();
                switch (SharedLib.IsNoCallDetected)
                {
                    case NoCallEventEnum.NoCarrier:
                        LblMsg.Text += " " + ResourcesText.MsgNoCarrier + " "; 
                        break;
                    case NoCallEventEnum.Busy:
                        LblMsg.Text += " " + ResourcesText.MsgBusy + " ";
                        break;
                    case NoCallEventEnum.NoDialTone:
                        LblMsg.Text += " " + ResourcesText.MsgNoTone + " ";
                        break;
                    case NoCallEventEnum.NoAnswer:
                        LblMsg.Text += " " + ResourcesText.MsgNoAnswer + " ";
                        break;
                    default:
                        LblMsg.Text += " " + ResourcesText.MsgCancel + " ";
                        break;
                }
                LblMsg.Text += LblNumDial.Text;
                LblNumDial.Text = "";
                BntCall.Text = ResourcesText.WordCall;
                SharedLib.ModemGSM.HangUp();
                _StatusPhone = StatusPhoneEnum.Ready;
                SharedLib.IsNoCallDetected = NoCallEventEnum.None;
                Thread.Sleep(100);
            }
        }

        private void BntM1_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                string num = Properties.Settings.Default.DefaultPhoneM1Number;
                LblNumDial.Text = "";
                for (int i = 0; i < num.Length; i++)
                {
                    LblNumDial.Text += num.Substring(i, 1);
                    Thread.Sleep(50);
                    Application.DoEvents();
                }
                this.BntCall.Focus();
            }
        }

        private void BntM2_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                string num = Properties.Settings.Default.DefaultPhoneM2Number;
                LblNumDial.Text = "";
                for (int i = 0; i < num.Length; i++)
                {
                    LblNumDial.Text += num.Substring(i, 1);
                    Thread.Sleep(50);
                    Application.DoEvents();
                }
                BntCall.Focus();
            }
        }

        private void BntM3_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                string num = Properties.Settings.Default.DefaultPhoneM3Number;
                LblNumDial.Text = "";
                for (int i = 0; i < num.Length; i++)
                {
                    LblNumDial.Text += num.Substring(i, 1);
                    Thread.Sleep(50);
                    Application.DoEvents();
                }
                BntCall.Focus();
            }
        }

        private void BntOPE_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                if (LblNumDial.Text != "")
                {
                    string num = Properties.Settings.Default.DefaultPhoneMDDD;
                    if (LblNumDial.Text.StartsWith(num))
                    {
                        return;
                    }
                    num += LblNumDial.Text;
                    LblNumDial.Text = num;
                }
            }
        }

        private void BntOkSettings_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                Properties.Settings.Default.DefaultPhoneM1Number = TxtNumber1.Text;
                Properties.Settings.Default.DefaultPhoneM2Number = TxtNumber2.Text;
                Properties.Settings.Default.DefaultPhoneM3Number = TxtNumber3.Text;
                Properties.Settings.Default.DefaultPhoneM1Name = TxtNome1.Text;
                Properties.Settings.Default.DefaultPhoneM2Name = TxtNome2.Text;
                Properties.Settings.Default.DefaultPhoneM3Name = TxtNome3.Text;
                Properties.Settings.Default.DefaultPhoneMDDD = TxtOPE.Text;
                SharedLib.SaveSettings();
                PnlSetting.Visible = false;
                BntSettings.Visible = true;
                toolTip.SetToolTip(BntM1, TxtNome1.Text != "" ? TxtNome1.Text : ResourcesText.WordMemory + " 1");
                toolTip.SetToolTip(BntM2, TxtNome2.Text != "" ? TxtNome2.Text : ResourcesText.WordMemory + " 2");
                toolTip.SetToolTip(BntM3, TxtNome3.Text != "" ? TxtNome3.Text : ResourcesText.WordMemory + " 3");
                toolTip.SetToolTip(BntOPE, TxtOPE.Text != "" ? ResourcesText.AddCode + " " + TxtOPE.Text : ResourcesText.AddCode + " " + ResourcesText.HostOperator );
            }
        }

        private void LstAgenda_MouseDown(object sender, MouseEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                if (e.Button == MouseButtons.Left && e.Clicks == 1)
                {
                    if (LstAgenda.SelectedIndex == -1) return;
                    this.DoDragDrop((PhoneBook)LstAgenda.SelectedItem, DragDropEffects.Move | DragDropEffects.Copy);
                }
                else if (e.Button == MouseButtons.Left && e.Clicks == 2)
                {
                    LblMsg.Visible = false;
                    if (LstAgenda.SelectedIndex == -1) return;
                    LblNumDial.Text = "";
                    string num = ((PhoneBook)LstAgenda.SelectedItem).Number;
                    for (int i = 0; i < num.Length; i++)
                    {
                        LblNumDial.Text += num.Substring(i, 1);
                        Thread.Sleep(50);
                        Application.DoEvents();
                    }
                    BntCall.Focus();
                }
            }
        }

        private void TxtNome1_DragDrop(object sender, DragEventArgs e)
        {
            LblMsg.Visible = false;
            PhoneBook pb = (PhoneBook)e.Data.GetData(typeof(PhoneBook));
            TxtNome1.Text = pb.Name;
            TxtNumber1.Text = pb.Number;
        }

        private void TxtNome1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void TxtNumber1_DragDrop(object sender, DragEventArgs e)
        {
            LblMsg.Visible = false;
            PhoneBook pb = (PhoneBook)e.Data.GetData(typeof(PhoneBook));
            TxtNome1.Text = pb.Name;
            TxtNumber1.Text = pb.Number;
        }

        private void TxtNumber1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void TxtNome2_DragDrop(object sender, DragEventArgs e)
        {
            LblMsg.Visible = false;
            PhoneBook pb = (PhoneBook)e.Data.GetData(typeof(PhoneBook));
            TxtNome2.Text = pb.Name;
            TxtNumber2.Text = pb.Number;
        }

        private void TxtNome2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void TxtNumber2_DragDrop(object sender, DragEventArgs e)
        {
            LblMsg.Visible = false;
            PhoneBook pb = (PhoneBook)e.Data.GetData(typeof(PhoneBook));
            TxtNome2.Text = pb.Name;
            TxtNumber2.Text = pb.Number;
        }

        private void TxtNumber2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void TxtNome3_DragDrop(object sender, DragEventArgs e)
        {
            LblMsg.Visible = false;
            PhoneBook pb = (PhoneBook)e.Data.GetData(typeof(PhoneBook));
            TxtNome3.Text = pb.Name;
            TxtNumber3.Text = pb.Number;
        }
        private void TxtNome3_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void TxtNumber3_DragDrop(object sender, DragEventArgs e)
        {
            LblMsg.Visible = false;
            PhoneBook pb = (PhoneBook)e.Data.GetData(typeof(PhoneBook));
            TxtNome3.Text = pb.Name;
            TxtNumber3.Text = pb.Number;
        }

        private void TxtNumber3_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void TxtNumber1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void TxtNumber2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void TxtNumber3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void TxtDDD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void LblNumDial_DragDrop(object sender, DragEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                string num = ((PhoneBook)e.Data.GetData(typeof(PhoneBook))).Number;
                LblNumDial.Text = "";
                for (int i = 0; i < num.Length; i++)
                {
                    LblNumDial.Text += num.Substring(i, 1);
                    Thread.Sleep(50);
                    Application.DoEvents();
                }
                BntCall.Focus();
            }
        }

        private void LblNumDial_DragEnter(object sender, DragEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void BntCall_DragDrop(object sender, DragEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                string num = ((PhoneBook)e.Data.GetData(typeof(PhoneBook))).Number;
                LblNumDial.Text = "";
                for (int i = 0; i < num.Length; i++)
                {
                    LblNumDial.Text += num.Substring(i, 1);
                    Thread.Sleep(50);
                    Application.DoEvents();
                }
                BntCall.Focus();
            }
        }

        private void BntCall_DragEnter(object sender, DragEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void FrmDial_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                if (e.KeyChar == '\b')
                {
                    if (LblNumDial.Text == "") return;
                    LblNumDial.Text = LblNumDial.Text.Substring(0, LblNumDial.Text.Length - 1);
                    return;
                }
                if (e.KeyChar == '#')
                {
                    LblNumDial.Text += e.KeyChar;
                    return;
                }
                if (e.KeyChar == '*')
                {
                    LblNumDial.Text += e.KeyChar;
                    return;
                }
                if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                {
                    LblNumDial.Text += e.KeyChar;
                    return;
                }
            }
        }
        private void FrmDial_KeyDown(object sender, KeyEventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                if (e.KeyData == Keys.Delete)
                {
                    if (LblNumDial.Text == "") return;
                    LblNumDial.Text = LblNumDial.Text.Substring(0, LblNumDial.Text.Length - 1);
                    return;
                }
                if (e.KeyData == Keys.Enter)
                {
                    BntCall.Focus();
                    BntCall.PerformClick();
                    return;
                }

            }
        }

        private void FrmDial_FormClosing(object sender, FormClosingEventArgs e)
        {
            SharedLib.ModemGSM.NoCallDetected -= ModemGSM_NoCallDetected;
        }

        private void timerCall_Tick(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.call)
            {
                _TmCall = _TmCall.Add(new TimeSpan(0, 0, 0, 0, 500));
                LblMsg.Text = _TmCall.Hours.ToString().PadLeft(2, '0') + ":" + _TmCall.Minutes.ToString().PadLeft(2, '0') + ":" + _TmCall.Seconds.ToString().PadLeft(2, '0');
                if (SharedLib.IsNoCallDetected != NoCallEventEnum.None)
                {
                    timerCall.Stop();
                    BntCall.PerformClick();
                }
            }
        }

        private void BntNoOPE_Click(object sender, EventArgs e)
        {
            if (_StatusPhone == StatusPhoneEnum.Ready)
            {
                LblMsg.Visible = false;
                if (LblNumDial.Text == "") return;
                if (LblNumDial.Text.Length >= Properties.Settings.Default.DefaultCodeOper.Length)
                {
                    string code = Properties.Settings.Default.DefaultCodeOper.ToUpper().Replace("X", "");
                    if (LblNumDial.Text.StartsWith(code))
                    {
                        LblNumDial.Text = LblNumDial.Text.Substring(Properties.Settings.Default.DefaultCodeOper.Length);
                    }
                }
            }
        }
    }
}
